/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.check;

/**
 *
 * @author zwlin
 */
public class CheckDesc {

	public static enum CheckType {
		Base64, Domain, Email, IntegerRange, Ip, Long, MD5, Phone, QQ, TimeDelay, TimeLater;
	}

	public static CheckDesc parse(String sDesc) {
		if (sDesc == null) {
			return null;
		}
		sDesc = sDesc.trim();
		if (sDesc.isEmpty()) {
			return null;
		}
		CheckType type = null;
		Object obj = null;
		switch (sDesc.charAt(0)) {
			case 'b'://Base64
				type = CheckType.Base64;
				break;
			case 'd'://Domain
				type = CheckType.Domain;
				break;
			case 'e':
				type = CheckType.Email;
				break;
			case 'r'://IntegerRange
				type = CheckType.IntegerRange;
				obj = Integer.parseInt(sDesc.substring(1));
				break;
			case 'i'://Ip
				type = CheckType.Ip;
				break;
			case 'l'://Long
				type = CheckType.Long;
				break;
			case 'm'://MD5
				if (sDesc.length() < 2) {
					return null;
				}
				type = CheckType.MD5;
				obj = sDesc.substring(1);
				break;
			case 'p':
				type = CheckType.Phone;
				break;
			case 'q':
				type = CheckType.QQ;
			case 't'://TimeXXX
				if (sDesc.length() < 3) {
					return null;
				}
				switch (sDesc.charAt(1)) {
					case 'd'://TimeDelay
						type = CheckType.TimeDelay;
						try {
							obj = Integer.parseInt(sDesc.substring(2));
						} catch (NumberFormatException e) {
							return null;
						}
						break;
					case 'l'://TimeLater
						type = CheckType.TimeLater;
						obj = sDesc.substring(2);
						break;
				}
				break;
			default:
				return null;
		}
		return new CheckDesc(type, obj);
	}

	public final CheckType type;
	public final Object param;

	public CheckDesc(CheckType type, Object param) {
		this.type = type;
		this.param = param;
	}

}
